<?php
//  +----------------------------------------------------------------------
//  | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//  | Licensed ( http://www.hdphp.cn/licenses/ )
//  | Author: Jack <sophia2152@qq.com>
//  | 官方网址: http://www.hdphp.cn
//  | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//  | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//  +----------------------------------------------------------------------
namespace hdphp\services;

use app\services\system\config\SystemConfigServices;
use hdphp\utils\Arr;
use think\facade\Config;

/**
 * 获取系统配置服务类
 * Class SystemConfigService
 * @package service
 */
class SystemConfigService
{
    /**
     * 缓存前缀字符
     */
    const CACHE_SYSTEM = 'system_config';

    /**
     * 过期时间
     */
    const EXPIRE_TIME = 30 * 24 * 3600;

    /**
     * @var int
     */
    protected static $relationId = 0;

    /**
     * @var int
     */
    protected static $type = 0;

    /**
     * Notes：获取配置缓存前缀
     * User：Jack
     * DateTime：2022/10/31 16:29
     * @return string
     */
    public static function getTag()
    {
        return Config::get('cache.stores.redis.tag_prefix') . 'cache_' . self::CACHE_SYSTEM;
    }

    /**
     * Notes：setStore
     * User：Jack
     * DateTime：2022/10/31 16:29
     * @param int $storeId
     * @return void
     */
    public static function setStore(int $storeId)
    {
        self::$relationId = $storeId;
        self::$type = 1;
    }

    /**
     * @param int $supplier
     */
    public static function setSupplier(int $supplier)
    {
        self::$relationId = $supplier;
        self::$type = 2;
    }

    /**
     * Notes：获取单个配置效率更高
     * User：Jack
     * DateTime：2022/10/31 16:28
     * @param string $key
     * @param string $default
     * @param bool   $isCaChe 是否获取缓存配置
     * @return mixed|string
     */
    public static function get(string $key, $default = '', bool $isCaChe = false)
    {
        $cacheName = self::CACHE_SYSTEM . ':' . $key . (self::$type ? '_' . self::$type : '') . (self::$relationId ? '_' . self::$relationId : '');
        $type = self::$type;
        $relationId = self::$relationId;
        $callable = function () use ($key, $type, $relationId) {
            event('get.config');
            /** @var SystemConfigServices $service */
            $service = app()->make(SystemConfigServices::class);
            return $service->getConfigValue($key, $type, $relationId);
        };

        try {
            if ($isCaChe) {
                return $callable();
            }
            $value = CacheService::redisHandler(self::getTag())->remember($cacheName, $callable, self::EXPIRE_TIME);
            self::$relationId = 0;
            self::$type = 0;
            return $value;
        } catch (\Throwable $e) {
            return $default;
        }
    }

    /**
     * Notes：获取多个配置
     * User：Jack
     * DateTime：2022/10/31 16:29
     * @param array $keys    示例 [['appid','1'],'appkey']
     * @param bool  $isCaChe 是否获取缓存配置
     * @return array|mixed
     */
    public static function more(array $keys, bool $isCaChe = false)
    {
        $cacheName = self::CACHE_SYSTEM . ':' . md5(implode(',', $keys) . (self::$type ? '_' . self::$type : '') . (self::$relationId ? '_' . self::$relationId : ''));
        $type = self::$type;
        $relationId = self::$relationId;
        $callable = function () use ($keys, $type, $relationId) {
            /** @var SystemConfigServices $service */
            $service = app()->make(SystemConfigServices::class);
            return Arr::getDefaultValue($keys, $service->getConfigAll($keys, $type, $relationId));
        };

        try {
            if ($isCaChe)
                return $callable();

            $value = CacheService::redisHandler(self::getTag())->remember($cacheName, $callable, self::EXPIRE_TIME);
            self::$relationId = 0;
            self::$type = 0;
            return $value;
        } catch (\Throwable $e) {
            return Arr::getDefaultValue($keys);
        }
    }

    /**
     * Notes：清空配置缓存
     * User：Jack
     * DateTime：2022/10/31 16:30
     * @return bool
     */
    public static function clear()
    {
        try {
            return CacheService::redisHandler(self::getTag())->clear();
        } catch (\Throwable $e) {
            \think\facade\Log::error('清空配置缓存失败：原因：' . $e->getMessage());
            return false;
        }
    }
}